//
//  IconsModel.m
//  AffectionBee
//
//  Created by 王炯 on 16/8/20.
//  Copyright © 2016年 chivalrous. All rights reserved.
//

#import "IconsAndActivitiesModel.h"
#import "NetworkTool.h"

@implementation IconsAndActivitiesModel


+(instancetype)IconsModelWithDict:(NSDictionary *)dict
{
    IconsAndActivitiesModel *model = [[IconsAndActivitiesModel alloc] init];
    
    
    [model setValuesForKeysWithDictionary:dict];
    
    

    return model;
}

//处理未用到的键值
-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    
}


+ (void)GetIconDataArrayWithDataType:(NSString *)dataType  Sucess:(void (^)(NSArray<IconsAndActivitiesModel *> *dataArray))successBlock Error:(void (^)())errorBlock
{
    
    //(2)url
    NSString *urlString = @"http://iosapi.itcast.cn/loveBeen/focus.json.php";
    
    //(3)参数
    NSDictionary *param = @{@"call":@1};
    
    
    //(4)POST方式请求数据
    [[NetworkTool sharedManager] oauthLoadDataWithNetworkToolType:NetworkToolTypePOST UrlString:urlString parameters:param SuccessBlock:^(NSDictionary *responseObject) {
        
        
        //(0)取验证码code
        NSNumber *codeNum = responseObject[@"code"];
        
        NSInteger code = [codeNum integerValue];
        
        //(1)返回data字典
        NSDictionary *dataDict = responseObject[@"data"];
        
        //(2)返回data字典中的icons数组
        if (dataDict[dataType] && code == 0)
        {

            //(3)取数组
            NSArray *iconsArray = dataDict[dataType];
            
            //(4)声明一个可变数组
            NSMutableArray *mArray = [NSMutableArray array];
            
            //(5)遍历数组，字典转模型
            [iconsArray enumerateObjectsUsingBlock:^(NSDictionary  *obj, NSUInteger idx, BOOL * _Nonnull stop) {
               
                //声明一个model,字典转模型
                IconsAndActivitiesModel *model = [IconsAndActivitiesModel IconsModelWithDict:obj];
                
                //添加到可变数组中
                [mArray addObject:model];
                
            }];
            
            //(6)调用block返回模型数组
            if (successBlock)
            {
                successBlock(mArray.copy);
            }
        }
        else
        {
            //调用错的block
            if (errorBlock)
            {
                errorBlock();
            }
        }
        
    } failure:^(NSError *error) {
        
        
        //调用错的block
        if (errorBlock)
        {
            errorBlock();
        }
        
    }];
    
}

+ (void)GetIconDataArraySucess:(void (^)(NSDictionary *dataDict))successBlock Error:(void (^)())errorBlock
{
    
    //(2)url
    NSString *urlString = @"http://iosapi.itcast.cn/loveBeen/focus.json.php";
    
    //(3)参数
    NSDictionary *param = @{@"call":@1};
    
    
    //(4)POST方式请求数据
    [[NetworkTool sharedManager] oauthLoadDataWithNetworkToolType:NetworkToolTypePOST UrlString:urlString parameters:param SuccessBlock:^(NSDictionary *responseObject) {
        
        //(0)取验证码code
        NSNumber *codeNum = responseObject[@"code"];
        
        NSInteger code = [codeNum integerValue];
        //(1)返回data字典
        NSDictionary *dataDict = responseObject[@"data"];
        
        NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
        //(2)返回data字典中的icons数组
        if (dataDict[@"focus"] && code == 0)
        {
            
            //(3)取数组
            NSArray *iconsArray = dataDict[@"focus"];
            
            //(4)声明一个可变数组
            NSMutableArray *mArray = [NSMutableArray array];
            
            //(5)遍历数组，字典转模型
            [iconsArray enumerateObjectsUsingBlock:^(NSDictionary  *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                //声明一个model,字典转模型
                IconsAndActivitiesModel *model = [IconsAndActivitiesModel IconsModelWithDict:obj];
                
                //添加到可变数组中
                [mArray addObject:model];
                
            }];
            [mDict setObject:mArray.copy forKey:@"focus"];
        }
        
        if (dataDict[@"activities"] && code == 0)
        {
            
            //(3)取数组
            NSArray *iconsArray = dataDict[@"activities"];
            
            //(4)声明一个可变数组
            NSMutableArray *mArray = [NSMutableArray array];
            
            //(5)遍历数组，字典转模型
            [iconsArray enumerateObjectsUsingBlock:^(NSDictionary  *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                //声明一个model,字典转模型
                IconsAndActivitiesModel *model = [IconsAndActivitiesModel IconsModelWithDict:obj];
                
                //添加到可变数组中
                [mArray addObject:model];
                
            }];
            [mDict setObject:mArray.copy forKey:@"activities"];
        }
        
        if (dataDict[@"icons"] && code == 0)
        {
            
            //(3)取数组
            NSArray *iconsArray = dataDict[@"icons"];
            
            //(4)声明一个可变数组
            NSMutableArray *mArray = [NSMutableArray array];
            
            //(5)遍历数组，字典转模型
            [iconsArray enumerateObjectsUsingBlock:^(NSDictionary  *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                //声明一个model,字典转模型
                IconsAndActivitiesModel *model = [IconsAndActivitiesModel IconsModelWithDict:obj];
                
                //添加到可变数组中
                [mArray addObject:model];
                
            }];
            [mDict setObject:mArray.copy forKey:@"icons"];
        }
        //(6)调用block返回模型数组
        if (successBlock)
        {
            successBlock(mDict.copy);
        }
        
    } failure:^(NSError *error) {
        
        //调用错的block
        if (errorBlock)
        {
            errorBlock();
        }
    }];
}


@end
